<?php

namespace app\admin\controller;

use think\Request;
use think\Controller;
use think\facade\Db;
use app\admin\model\Reimbursement as ment;

class Reimbursement extends BaseAdmin
{
    //列表
    public function Reimbursement()
    {
        $ment = new ment();
        //$res = $ment::where('is_delete',0)->order(['update_time desc','create_time desc'])->paginate(20);
        $res = $ment::where('is_delete',0)->orderRaw('IFNULL(update_time,create_time) desc')->paginate(20);
        $count = $ment::where('is_delete',0)->count();
        $this->assign(['res'=>$res,'count'=>$count]);
        return view('list');
    }

    //待处理
    public function waitStatus()
    {
        $ment = new ment();
        $res = $ment::where('status',0)->where('is_delete',0)->orderRaw('IFNULL(update_time,create_time) desc')->paginate(20);
        $count = $ment::where('is_delete',0)->where('status',0)->count();
        $this->assign(['res'=>$res,'count'=>$count]);
        return view('list');
    }

    //已处理
    public function alreadyStatus()
    {
        $ment = new ment();
        $res = $ment::where('status',1)->where('is_delete',0)->orderRaw('IFNULL(update_time,create_time) desc')->paginate(20);
        $count = $ment::where('status',1)->where('is_delete',0)->count();
        $this->assign(['res'=>$res,'count'=>$count]);
        return view('list');
    }

    //添加
    public function addMent()
    {
        if (request()->ispost()) {
           //input('electronic_invoice')
            $data = [
                'sex'         => !empty(input('sex')) ? trim(input('sex')) : '',
                'name'        => !empty(match_chinese(input('name'))) ? trim(match_chinese(input('name'))) : '',
                'content'     => !empty(match_chinese(input('content'))) ? trim(match_chinese(input('content'))) : '',
                'money'       => !empty(input('money')) ? trim(input('money')) : '',
                'status'      => !empty(input('status')) ? trim(input('status')) : '',
                'create_time' => time(),
                'electronic_invoice' => input('electronic_invoice'),
                'reimbursement' => input('reimbursement'),
            ];

            $ment = new ment();
            $add = $ment::insert($data);
            if ($add) {
                return json(['code' => 200, 'msg' => '添加成功']);
            } else {
                return json(['code' => 500, 'msg' => '添加失败']);
            }
        }
        return view('add');
    }

    //修改
    public function editMent()
    {
        $ment = new ment();
        if(request()->ispost()){
            $data = input('post.');
            $data['update_time'] = time();
            $edit = $ment->allowField(true)->update($data);
            if($edit){
                return json(['code' => 200,'msg' => '修改成功']);
            }else{
                return json(['code' => 500,'msg' => '修改失败']);
            }
        }
        $user = $ment::find(input('id'));
        $this->assign(['user' => $user]);
        return view('edit');
    }

    //删除 到回收站
    public function del()
    {
        $ment = new ment();
        $id = (int)input('post.id');
        $list = ['is_delete'=> 1];
        $res = $ment::where('id',$id)->update($list);
        if (!$res) {
            exit(json_encode(array('code' => 1, 'msg' => '操作失败')));
        }
        exit(json_encode(array('code' => 0, 'msg' => '已放入回收站')));
    }

    //回收站列表
    public function recycleBin(){
        $ment = new ment();
        $res = $ment::where('is_delete',1)->orderRaw('IFNULL(update_time,create_time) desc')->paginate(20);
        $count = $ment::where('is_delete',1)->count();
        $this->assign(['res'=>$res,'count'=>$count]);
        return view('recycle');
    }

    //回收站删除
    public function recycleBinDel(){
        $ment = new ment();
        $id = (int)input('post.id');
        //删除数据库记录之前先删除对应文件
        $deletePictureCompletely = $ment::where('id',$id)->find();
        $electronic_invoice = $_SERVER['DOCUMENT_ROOT'].$deletePictureCompletely['electronic_invoice'];
        $reimbursement = $_SERVER['DOCUMENT_ROOT'].$deletePictureCompletely['reimbursement'];
        if(file_exists($electronic_invoice)){
            @unlink($electronic_invoice);
        }else{
            echo '文件不存在';die;
        }
        if(file_exists($reimbursement)){
            @unlink($reimbursement);
        }else{
            echo '文件不存在';die;
        }
        $res = $ment::where('id',$id)->delete();
        if (!$res) {
            exit(json_encode(array('code' => 1, 'msg' => '删除失败')));
        }
        exit(json_encode(array('code' => 0, 'msg' => '删除成功')));
    }


    //图片上传
    public function upload(){
        $file = request()->file('file');
        if($file){
            $info = $file->validate(['size'=>999999,'ext'=>'jpg,png'])->move( '../public/uploads');
            if($info){
                $path = '/uploads/'.$info->getSaveName();
                $path = str_replace('\\','/',$path);
                return json(['code'=>1,'msg'=>'上传成功','url'=>$path]);
            }else{
                return json(['code'=>0,'msg'=>$file->getError()]);
            }
        }
    }

}